package com.zcgame.base.utils

import java.io.UnsupportedEncodingException
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import kotlin.experimental.and

/**
 *@author:huangLJ
 *@date:2021/5/27 19:07
 */
object PasswordUtil {
    /**
     * md5转换工具
     */
    fun md5(string: String): String? {
        val hash: ByteArray
        hash = try {
            MessageDigest.getInstance("MD5").digest(
                string.toByteArray(charset("UTF-8"))
            )
        } catch (e: NoSuchAlgorithmException) {
            throw RuntimeException("Huh, MD5 should be supported?", e)
        } catch (e: UnsupportedEncodingException) {
            throw RuntimeException("Huh, UTF-8 should be supported?", e)
        }
        val hex = StringBuilder(hash.size * 2)
        for (b in hash) {
            if (b.toInt() and 0xFF < 0x10) hex.append("0")
            hex.append(Integer.toHexString((b.toInt() and 0xFF).toInt()))
        }
        return hex.toString()
    }
}

fun main() {
    val md5 = PasswordUtil.md5("huanglj")
    print(md5)
}